package com.zxy.hotel.util;

import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * @author 14995
 * @description
 * @create 2021-12-12 下午 9:20
 */
public class JwtUtil {
    //token有效时长
    private static final long tokenExpiration = 24 * 60 * 60 * 1000;
    //编码秘钥
    private static final String tokenSignKey = "jiusiadmintokensignkey";

    //1 使用jwt根据用户名生成token
    public static String createToken(String username) {
        return Jwts.builder().setSubject(username)
                .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
                .signWith(SignatureAlgorithm.HS512, tokenSignKey).compressWith(CompressionCodecs.GZIP).compact();
    }

    //2 根据token字符串得到用户名
    public static String getUserInfoFromToken(String token) {
        return Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token).getBody().getSubject();
    }
}
